package service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import domain.Goods;
import domain.Order;
import domain.OrderList;
import domain.Zkl;
import factory.DaoFactory;
import service.Ser;
import util.IuimSesssionFactory;
import util.JDBCUtil;

public class SerImpl implements Ser {

	@Override
	public Goods getOneGoodsInfo(String txm) {
		// TODO Auto-generated method stub
		String hql = " from Goods g where g.txm = ? ";
		Object[] param = { txm };
		Goods goods = DaoFactory.getGoodsDaoInstance().get(hql, param);
		return goods;
	}

	@Override
	public HashMap<Integer, Float> getAllZk() {
		// TODO Auto-generated method stub
		String hql = " from Zkl ";
		List<Zkl> zkls = DaoFactory.getZklDaoInstance().find(hql);
		HashMap<Integer, Float> zklsHash = new HashMap<Integer, Float>();
		for (Zkl zkl : zkls) {
			zklsHash.put(zkl.getId(), zkl.getZk());
		}
		return zklsHash;
	}

	@Override
	public int addOrder(Order order) {
		// TODO Auto-generated method stub
		DaoFactory.getOrderDaoInstance().save(order);
		String hql = "select * from tb_order o where o.id = (select MAX(id) from tb_order)";
		Session session = null;
		session = IuimSesssionFactory.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Query query = session.createSQLQuery(hql).addEntity(Order.class);
		transaction.commit();
		@SuppressWarnings("unchecked")
		List<Order> orders = query.list();
		session.close();
		return orders.get(0).getId();
	}

	@Override
	public void updateGoods(Goods goods) {
		// TODO Auto-generated method stub
		DaoFactory.getGoodsDaoInstance().update(goods);
	}

	@Override
	public void updateOrder(Order order) {
		// TODO Auto-generated method stub
		DaoFactory.getOrderDaoInstance().update(order);
	}

	@Override
	public void addOrderList(OrderList orderList) {
		// TODO Auto-generated method stub
		DaoFactory.getOrderListDaoInstance().save(orderList);
	}

	@Override
	public List<Zkl> getAllZkls() {
		// TODO Auto-generated method stub
		String hql = " from Zkl ";
		List<Zkl> zkls = DaoFactory.getZklDaoInstance().find(hql);
		return zkls;
	}

	@Override
	public int addZkl(Zkl zkl) {
		// TODO Auto-generated method stub
		DaoFactory.getZklDaoInstance().save(zkl);
		List<Zkl> zkls = getAllZkls();
		return zkls.get(zkls.size() - 1).getId();
	}

	@Override
	public void updateZkl(Zkl zkl) {
		// TODO Auto-generated method stub
		DaoFactory.getZklDaoInstance().update(zkl);
	}

	@Override
	public void addGoods(Goods goods) {
		// TODO Auto-generated method stub
		DaoFactory.getGoodsDaoInstance().save(goods);
	}

	@Override
	public List<Order> getOneMonthOrders(String date1, String date2) {
		// TODO Auto-generated method stub
		String hql = "select * from tb_order o where o.va_date >= '" + date1 + "' and o.va_date <= '" + date2 + "' ";
		Session session = null;
		session = IuimSesssionFactory.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Query query = session.createSQLQuery(hql).addEntity(Order.class);
		transaction.commit();
		@SuppressWarnings("unchecked")
		List<Order> orders = query.list();
		session.close();
		return orders;
	}

	@Override
	public List<Order> getTwntyOrders(int page) {
		// TODO Auto-generated method stub
		String hql = " select * from tb_order order by id DESC limit " + (page - 1) * 20 + ",20 ";
		Session session = null;
		session = IuimSesssionFactory.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Query query = session.createSQLQuery(hql).addEntity(Order.class);
		transaction.commit();
		@SuppressWarnings("unchecked")
		List<Order> orders = query.list();
		session.close();
		return orders;
	}

	@Override
	public List<Goods> getTwntyGoods(int page) {
		// TODO Auto-generated method stub
		String hql = " select * from tb_goods order by va_month DESC,va_txm limit " + (page - 1) * 20 + ",20 ";
		Session session = null;
		session = IuimSesssionFactory.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Query query = session.createSQLQuery(hql).addEntity(Goods.class);
		transaction.commit();
		@SuppressWarnings("unchecked")
		List<Goods> list = query.list();
		session.close();
		return list;
	}

	@Override
	public long getOrdersCount() {
		JDBCUtil jdbc = new JDBCUtil();// 创建数据库服务对象
		List<Object> result = new ArrayList<Object>();
		String sql = "SELECT COUNT(*) as count FROM tb_order ";
		result = jdbc.excuteQuery(sql, null);
		Object object = new Object();
		long count = 0;
		for (int i = 0, l = result.size(); i < l; i++) {
			object = result.get(i);
			@SuppressWarnings("rawtypes")
			Map map = (Map) object;
			count = (long) map.get("count");
		}
		return count;
	}

	@Override
	public List<OrderList> getOneOrderList(int order) {
		// TODO Auto-generated method stub
		String hql = " from OrderList l where l.order = ? ";
		Object[] param = { order };
		List<OrderList> orderLists = DaoFactory.getOrderListDaoInstance().find(hql, param);
		return orderLists;
	}

	@Override
	public void getPreMonthNumber(String txm) {
		// TODO Auto-generated method stub
		String hql = "select * from tb_orderlist o where o.va_txm = \""+txm+"\" and o.va_date > DATE_SUB(NOW(),INTERVAL 1 MONTH) ";
		Session session = null;
		session = IuimSesssionFactory.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Query query = session.createSQLQuery(hql).addEntity(OrderList.class);
		transaction.commit();
		@SuppressWarnings("unchecked")
		List<OrderList> list = query.list();
		session.close();
		int result = 0;
		if (list != null && list.size() > 0) {
			for (OrderList oList : list)
				result += oList.getSl();
		}
		Goods goods = getOneGoodsInfo(txm);
		goods.setMonth(result);
		DaoFactory.getGoodsDaoInstance().update(goods);
	}

	@Override
	public void caculatePreMonth() {
		// TODO Auto-generated method stub
		String hql = "update tb_goods o set o.va_month = 0 ";
		Session session = null;
		session = IuimSesssionFactory.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Query query = session.createSQLQuery(hql);
		query.executeUpdate();
		transaction.commit();
		hql = " select * from tb_goods where va_txm in ( select va_txm from tb_orderlist where va_date > DATE_SUB(NOW(),INTERVAL 1 MONTH) ) ";
		transaction = session.beginTransaction();
		query = session.createSQLQuery(hql).addEntity(Goods.class);
		transaction.commit();
		@SuppressWarnings("unchecked")
		List<Goods> goods = query.list();
		session.close();
		for (Goods dGoods : goods) {
			getPreMonthNumber(dGoods.getTxm());
		}
	}

	@Override
	public int countGoods() {
		// TODO Auto-generated method stub
		String hql = " select count(*) from Goods ";
		int result = DaoFactory.getGoodsDaoInstance().count(hql);
		return result;
	}

}
